﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Cwt;

namespace WebApp.Management._Summary
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ICwtService service = Global.Get<ICwtService>();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    Guid id = new Guid(Request.QueryString["id"]);

                    Summary summary = service.FindSummary(id);
                    if (summary != null)
                    {
                        _ctlHiddenId.Value = summary.Id.ToString("N");
                        _ctlEditTitle.Text = summary.Title;
                        _ctlEditOrder.Text = summary.Order.ToString();
                        _ctlEditContent.Text = summary.Content;
                    }
                }
                _ctlRepeater.DataSource = service.FindSummaries();
                _ctlRepeater.DataBind();
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            string title = _ctlEditTitle.Text;
            int order = Convert.ToInt32(_ctlEditOrder.Text);
            string content = _ctlEditContent.Text;

            Summary summary = new Summary();
            if (!string.IsNullOrEmpty(_ctlHiddenId.Value))
            {
                summary.Id = new Guid(_ctlHiddenId.Value);
            }
            else
            {
                summary.Id = Guid.NewGuid();
            }
            summary.Title = title;
            summary.Order = order;
            summary.Content = content;
            ICwtService service = Global.Get<ICwtService>();
            if (service.SaveSummary(summary))
            {
                Response.Write("<script>alert('保存成功!');window.location.href='./Update.aspx';</script>");
            }
            else
            {
                Response.Redirect("../Message.aspx");
            }
        }

        protected void _ctlRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                Guid id = new Guid(e.CommandArgument.ToString());
                ICwtService service = Global.Get<ICwtService>();
                Summary summary = service.FindSummary(id);
                if (summary != null)
                {
                    if (service.DeleteSummary(summary))
                    {
                        Response.Write("<script>alert('删除成功!');window.location.href='./Update.aspx';</script>");
                    }
                }
            }
            else if (e.CommandName == "update")
            {
                Guid id = new Guid(e.CommandArgument.ToString());
            }
        }
    }
}